package com.stenio.fancat.controller.shop;

import com.baomidou.mybatisplus.plugins.Page;
import com.stenio.fancat.controller.Result;
import com.stenio.fancat.domain.PageList;
import com.stenio.fancat.domain.baby.BabyAccount;
import com.stenio.fancat.domain.baby.BabyIconImage;
import com.stenio.fancat.domain.behavior.BehaviorOperationImage;
import com.stenio.fancat.domain.shop.FcRedOrder;
import com.stenio.fancat.form.baby.BabyAccountForm;
import com.stenio.fancat.mapper.baby.BabyIconImageMapper;
import com.stenio.fancat.service.shop.PhotoService;
import com.stenio.fancat.vo.BabyAccountVO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuan
 * @create 2019-02-13 11:50
 */
@RestController
@RequestMapping("photo")
public class PhotoController {

    @Autowired
    private PhotoService photoService;

    @Autowired
    private BabyIconImageMapper babyIconImageMapper;

    @GetMapping("get/{id}")
    public Result get(@PathVariable String id,Integer pageNo,Integer pageSize) {
        Result result = new Result();
        PageList byBabyId = photoService.getByBabyId(id, pageNo, pageSize);
        if(byBabyId == null) {
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        result.setData(byBabyId);
        return result;
    }

    @PostMapping("findbabyInfo")
    public Result findbabyInfo(BabyAccountForm babyAccountForm) {
        Result result = new Result();
        final Page<BabyAccount> babyAccountPage = photoService.selectBabyInfo(babyAccountForm);
        Map<String,Object> data = new HashMap<>();
        if(babyAccountPage != null) {
            data.put("current",babyAccountPage.getCurrent());
            data.put("pages",babyAccountPage.getPages());
            data.put("size",babyAccountPage.getSize());
            data.put("total",babyAccountPage.getTotal());
            List<BabyAccountVO> babyAccountVOS = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(babyAccountPage.getRecords())) {
                babyAccountPage.getRecords().stream().forEach(baby->{
                    babyAccountVOS.add(babyToVo(baby));
                });
            }
            data.put("records",babyAccountVOS);
        }

        result.setData(data);
        return result;
    }

    private BabyAccountVO babyToVo(BabyAccount babyAccount) {
        BabyAccountVO babyAccountVO = new BabyAccountVO();
        BeanUtils.copyProperties(babyAccount,babyAccountVO);
        BabyIconImage babyIconImage = babyIconImageMapper.selectByBid(babyAccount.getBabyId());
        babyAccountVO.setImage(babyIconImage.getImage());
        return babyAccountVO;
    }

}
